package com.songtech.uitest.module.deliver;

import com.songtech.uitest.components.IBrowserDriverFactory;
import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.*;
import com.songtech.uitest.module.propose.ProposeSubmit;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class DeliverSubmit {

    @Autowired
    @Qualifier("browserDriver")
    private IBrowserDriverFactory driverCreater;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private KakongHandler kakongHandler;

    @Autowired
    private LogInAndOutHandler logInAndOutHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(DeliverSubmit.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    /**
     * 交付确认提交。参数指定了是否同意提交和谁来操作。
     * @param step 类似"00"，十位代表提交确认是否同意；个位代表谁登录：0为主账号登录，其它为辅账号登录
     * @throws Exception
     */
    public void deliverOneSubmit(String step) throws Exception{

        boolean approval = stepParser.parseApproval(step);
        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //交付提交确认
        //菜单打开交付确认页面
        menuHandler.clickMenuItem(driver, "铁塔建设", "交付确认", "");
        //查询此单
        driver.switchTo().frame("iframeundefined");
        driver.findElement(By.id("keywords")).sendKeys((indent.getMobileId()));

        driver.findElement(By.xpath("//button[text()='查询']")).click();

        Thread.sleep(6000);
        //搜索并进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='提交']")).click();

        //打开并转到交付确认提交详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到需求确认提交详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);

        driver.switchTo().frame(frame);


        driver.findElement(By.id("auditNote")).sendKeys("提交此单去审核");

        Select status = new Select(driver.findElement(By.id("status")));
        status.selectByVisibleText(approval ? "同意订单交付" : "不同意订单交付");

        driver.findElement(By.id("remark")).sendKeys(approval ? "同意此交付结果" : "不同意此交付结果");

        driver.findElement(By.id("saveSet")).click();


        Thread.sleep(3000);

        kakongHandler.checkAndGo(driver);

        Thread.sleep(5000);

        //提交情况提示框出现,关掉它
        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        Thread.sleep(2000);

        driver.switchTo().defaultContent();

        String agree = approval ? "同意" : "不同意";
        logger.info(space.getSpace() + "交付确认提交成功，确认意见为" + agree);

    }


    public void deliver3OneSubmit(String step) throws Exception{

        boolean approval = stepParser.parseApproval(step);
        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //三方交付提交确认
        //菜单打开三方交付确认页面
        menuHandler.clickMenuItem(driver, "三方建设", "交付确认", "");
        //查询此单
        driver.switchTo().frame("iframeundefined");
        driver.findElement(By.id("keywords")).sendKeys((indent.getMobileId()));

        driver.findElement(By.xpath("//button[text()='查询']")).click();

        Thread.sleep(6000);
        //搜索并进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='确认']")).click();

        //打开并转到三方交付确认提交详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到三方交付确认提交详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);

        driver.switchTo().frame(frame);


        driver.findElement(By.id("auditNote")).sendKeys("提交此单去审核");

        Select status = new Select(driver.findElement(By.id("status")));
        status.selectByVisibleText(approval ? "同意第三方订单交付" : "不同意第三方订单交付");

        driver.findElement(By.id("remark")).sendKeys(approval ? "同意此三方交付结果" : "不同意此三方交付结果");

        driver.findElement(By.id("saveSet")).click();


        Thread.sleep(3000);

        kakongHandler.checkAndGo(driver);

        Thread.sleep(5000);

        //提交情况提示框出现,关掉它
        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        Thread.sleep(2000);

        driver.switchTo().defaultContent();

        String agree = approval ? "同意" : "不同意";
        logger.info(space.getSpace() + "三方交付确认提交成功，确认意见为" + agree);

    }


}
